package book;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 读写锁
 * @author：涂有
 * @date 2017年3月1日 下午3:19:38
 */
public class ReentrantReadWriteLockTest {

	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	public void read(){
		
//		lock.readLock().lock();
		lock.writeLock().lock();
		try{
			System.out.println("获得锁：" + Thread.currentThread().getName());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}finally{
			System.out.println("释放锁：" + Thread.currentThread().getName());
//			lock.readLock().unlock();
			lock.writeLock().unlock();
		}
	}
	
	public static void main(String[] args) {
		
		final ReentrantReadWriteLockTest test = new ReentrantReadWriteLockTest();
		for(int i = 0; i < 10; i++){
			
			new Thread(){
				
				public void run() {
					
					test.read();
				};
			}.start();
		}
	}
}
